package com.yuexunit.finance.statement.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.statement.entity.EarningSummaryEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
*@version 创建时间：2017年12月6日 上午9:54:48
*@author wenwenlong
*类说明
*/

@Implements(EarningSummaryService.class)
public class EarningSummaryServiceImpl implements EarningSummaryService {
	
	@Override
	@Transactional
	public void createEarningSummary(EarningSummaryEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDt(DateUtils.now());
		entity.setUpdateDt(DateUtils.now());
		PersistUtil.insertOrUpdate(entity);
	}
	
	@Override
	public EarningSummaryEntity inquireEarningSummarys() {
		DetachedCriteria criteria = DetachedCriteria.forClass(EarningSummaryEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.addOrder(Order.asc("earningSummaryId"));
		List<EarningSummaryEntity> list = PersistUtil.get(criteria);
		Integer last = list.size() - 1;
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(last);
		}
	}

	@Override
	public List<EarningSummaryEntity> inquireEarningSummaryList(Integer summaryDate) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EarningSummaryEntity.class);
		criteria.add(Restrictions.eq("summaryDate", summaryDate));
		return PersistUtil.get(criteria);
	}
}
